package com.yunhai.api.controller;

import com.yunhai.common.core.controller.BaseController;
import com.yunhai.common.core.domain.AjaxResult;
import com.yunhai.common.enums.LanuageEnum;
import com.yunhai.common.utils.StringUtils;
import com.yunhai.system.domain.News;
import com.yunhai.system.service.NewsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@RestController
@RequestMapping("/api/news")
public class NewsController extends BaseController {

    @Autowired
    private NewsService newsService;


    /**
     * 根据分类获取媒体消息文章
     *
     * @param param
     * @return
     */
    @PostMapping("/getNewsByType")
    public AjaxResult getNewsByType(HttpServletRequest httpServletRequest, @RequestBody Map<String, Object> param) {
        String language = httpServletRequest.getHeader("yh-language");
        if (StringUtils.isBlank(language)) {
            language = LanuageEnum.CH.getCode().toString();
        }
        param.put("language", language);
        return AjaxResult.success(newsService.getNewsByType(param));
    }


    /**
     * 根据文章ID获取文章详情
     * @param param
     * @return
     */
    @PostMapping("/getNewsById")
    public AjaxResult getNewsById(@RequestBody Map<String, Object> param){
        String newsId = param.get("newsId") != null ? param.get("newsId").toString() : null;
        if(StringUtils.isBlank(newsId)){
            return AjaxResult.error("请传文章ID");
        }
        News news = newsService.getNewsById(param.get("newsId").toString());
        if(news == null){
            return AjaxResult.error("该文章不存在");
        }
        return AjaxResult.success(news);
    }

}
